console.log("华夏·鉴 - 主脚本已加载");

// 粒子动效（仅非首页）
if (document.body.classList.contains('body-bg-ta1')) {
    window.addEventListener('DOMContentLoaded', () => {
        const container = document.getElementById('bg-particles');
        if (!container) return;
        const canvas = document.createElement('canvas');
        canvas.style.width = '100vw';
        canvas.style.height = '100vh';
        canvas.style.position = 'absolute';
        canvas.style.left = '0';
        canvas.style.top = '0';
        canvas.style.zIndex = '2';
        canvas.style.pointerEvents = 'none';
        container.appendChild(canvas);
        const ctx = canvas.getContext('2d');
        function resize() {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
        }
        resize();
        window.addEventListener('resize', resize);
        // 粒子参数
        const particles = Array.from({length: 38}, () => ({
            x: Math.random() * canvas.width,
            y: Math.random() * canvas.height,
            r: 1.2 + Math.random() * 2.2,
            dx: -0.3 + Math.random() * 0.6,
            dy: -0.3 + Math.random() * 0.6,
            alpha: 0.18 + Math.random() * 0.22
        }));
        function draw() {
            ctx.clearRect(0,0,canvas.width,canvas.height);
            for (const p of particles) {
                ctx.beginPath();
                ctx.arc(p.x, p.y, p.r, 0, 2*Math.PI);
                ctx.fillStyle = `rgba(58,90,108,${p.alpha})`;
                ctx.shadowColor = '#fff';
                ctx.shadowBlur = 6;
                ctx.fill();
                ctx.shadowBlur = 0;
                p.x += p.dx;
                p.y += p.dy;
                if (p.x < 0 || p.x > canvas.width) p.dx *= -1;
                if (p.y < 0 || p.y > canvas.height) p.dy *= -1;
            }
            requestAnimationFrame(draw);
        }
        draw();
    });
}

// 水墨动态背景（首页专用）
(function() {
  const canvas = document.getElementById('ink-bg-canvas');
  if (!canvas) return;
  const ctx = canvas.getContext('2d');
  let width = window.innerWidth;
  let height = window.innerHeight;
  function resize() {
    width = window.innerWidth;
    height = window.innerHeight;
    canvas.width = width;
    canvas.height = height;
  }
  resize();
  window.addEventListener('resize', resize);

  // 水墨晕染点参数
  const blobs = Array.from({length: 7}, (_,i) => ({
    x: Math.random()*width,
    y: Math.random()*height,
    r: 120+Math.random()*180,
    dx: -0.12+Math.random()*0.24,
    dy: -0.12+Math.random()*0.24,
    alpha: 0.10+Math.random()*0.13,
    t: Math.random()*Math.PI*2
  }));

  function draw() {
    ctx.clearRect(0,0,width,height);
    const isNight = document.body.classList.contains('night-mode');
    blobs.forEach(b => {
      // 晕染主色
      ctx.save();
      ctx.globalAlpha = b.alpha * (isNight ? 1.5 : 1);
      const grad = ctx.createRadialGradient(b.x, b.y, b.r*0.2, b.x, b.y, b.r);
      grad.addColorStop(0, isNight ? '#222' : '#3a5a6c');
      grad.addColorStop(0.5, isNight ? '#3a5a6c' : '#b0a084');
      grad.addColorStop(1, isNight ? 'rgba(34,34,34,0)' : 'rgba(58,90,108,0)');
      ctx.beginPath();
      ctx.arc(b.x, b.y, b.r, 0, Math.PI*2);
      ctx.closePath();
      ctx.fillStyle = grad;
      ctx.filter = 'blur(12px)';
      ctx.fill();
      ctx.restore();
    });
  }

  function animate() {
    blobs.forEach(b => {
      b.x += b.dx + Math.sin(b.t)*0.2;
      b.y += b.dy + Math.cos(b.t)*0.2;
      b.t += 0.008+Math.random()*0.004;
      // 边界反弹
      if (b.x < -b.r) b.x = width+b.r;
      if (b.x > width+b.r) b.x = -b.r;
      if (b.y < -b.r) b.y = height+b.r;
      if (b.y > height+b.r) b.y = -b.r;
    });
    draw();
    requestAnimationFrame(animate);
  }
  animate();

  // 夜间模式切换时重绘
  const obs = new MutationObserver(draw);
  obs.observe(document.body, {attributes:true, attributeFilter:['class']});
})();

document.addEventListener('DOMContentLoaded', () => {
    initializeScreenAnimation();
    loadRandomQuote();
    const main = document.querySelector('main');
    if(main) main.classList.add('fade-page-enter');
});

function initializeScreenAnimation() {
    const leftPanel = document.querySelector('.left-panel');
    const nameContainer = document.querySelector('.name-container');

    if (!leftPanel || !nameContainer) {
        console.error('Screen animation elements not found.');
        return;
    }

    const tl = gsap.timeline({ defaults: { ease: 'power3.out' } });

    tl.to(leftPanel, {
        y: '0%',
        duration: 1.2,
        ease: 'cubic-bezier(0.25, 1, 0.5, 1)' // A more refined ease for the scroll
    }).to(nameContainer, {
        x: 0,
        opacity: 1,
        duration: 1.1
    }, "-=0.8");
}

function loadRandomQuote() {
    const quoteElement = document.getElementById('daily-quote');
    const sourceElement = document.getElementById('quote-source');

    if (!quoteElement || !sourceElement) {
        console.error('Quote display elements not found.');
        return;
    }

    const quotes = [
        { text: "天行健，君子以自强不息。", source: "《周易》" },
        { text: "知己知彼，百战不殆。", source: "《孙子兵法》" },
        { text: "学而不思则罔，思而不学则殆。", source: "《论语》" },
        { text: "路漫漫其修远兮，吾将上下而求索。", source: "《离骚》" },
        { text: "天下大势，分久必合，合久必分。", source: "《三国演义》" }
    ];

    const randomIndex = Math.floor(Math.random() * quotes.length);
    const randomQuote = quotes[randomIndex];

    quoteElement.textContent = randomQuote.text;
    sourceElement.textContent = `—— ${randomQuote.source}`;
}

// 回到顶部按钮交互
(function() {
  const btn = document.getElementById('backToTopBtn');
  if (!btn) return;
  function checkScroll() {
    if(window.scrollY > 200) {
      btn.style.display = 'flex';
    } else {
      btn.style.display = 'none';
    }
  }
  window.addEventListener('scroll', checkScroll);
  checkScroll();
  btn.addEventListener('click', function() {
    window.scrollTo({top:0, behavior:'smooth'});
  });
})();

// 首页文化彩蛋气泡
(function() {
  const mainTitle = document.querySelector('.main-title');
  if (!mainTitle) return;
  let bubble;
  function showEgg() {
    if (bubble) return;
    bubble = document.createElement('div');
    bubble.className = 'cultural-egg-bubble';
    bubble.innerHTML = '文物小知识：玉璧，古代礼器，象征天地圆融。';
    mainTitle.parentElement.appendChild(bubble);
    setTimeout(()=>{bubble.style.opacity=1; bubble.style.pointerEvents='auto';}, 80);
    setTimeout(hideEgg, 4200);
  }
  function hideEgg() {
    if (bubble) {
      bubble.style.opacity = 0;
      setTimeout(()=>{if(bubble) {bubble.remove(); bubble=null;}}, 400);
    }
  }
  mainTitle.addEventListener('mouseenter', showEgg);
  mainTitle.addEventListener('mouseleave', hideEgg);
  // 首次进入自动弹出一次
  setTimeout(showEgg, 1200);
})(); 